Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents LstResults As System.Windows.Forms.ListBox Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents TxtTicket As System.Windows.Forms.TextBox Friend WithEvents TxtGov As System.Windows.Forms.TextBox Friend WithEvents Label3 As System.Windows.Forms.Label Friend WithEvents TxtTimes As System.Windows.Forms.TextBox Friend WithEvents Label4 As System.Windows.Forms.Label Friend WithEvents BtnSim As System.Windows.Forms.Button Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Button2 As System.Windows.Forms.Button Friend WithEvents TxtWinnings As System.Windows.Forms.TextBox Friend WithEvents Label5 As System.Windows.Forms.Label Friend WithEvents TxtTake As System.Windows.Forms.TextBox Friend WithEvents Label6 As System.Windows.Forms.Label Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox Friend WithEvents BtnWrite As System.Windows.Forms.Button Private Sub InitializeComponent() Me.Label1 = New System.Windows.Forms.Label Me.LstResults = New System.Windows.Forms.ListBox Me.Label2 = New System.Windows.Forms.Label Me.TxtTicket = New System.Windows.Forms.TextBox Me.TxtGov = New System.Windows.Forms.TextBox Me.Label3 = New System.Windows.Forms.Label Me.TxtTimes = New System.Windows.Forms.TextBox Me.Label4 = New System.Windows.Forms.Label Me.BtnSim = New System.Windows.Forms.Button Me.Button1 = New System.Windows.Forms.Button Me.Button2 = New System.Windows.Forms.Button Me.TxtWinnings = New System.Windows.Forms.TextBox Me.Label5 = New System.Windows.Forms.Label Me.TxtTake = New System.Windows.Forms.TextBox Me.Label6 = New System.Windows.Forms.Label Me.RichTextBox1 = New System.Windows.Forms.RichTextBox Me.BtnWrite = New System.Windows.Forms.Button Me.SuspendLayout() ' 'Label1 ' Me.Label1.AutoSize = True Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 24.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.Location = New System.Drawing.Point(120, 8) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(287, 40) Me.Label1.TabIndex = 0 Me.Label1.Text = "Lottery Simulation" ' 'LstResults ' Me.LstResults.Location = New System.Drawing.Point(296, 64) Me.LstResults.Name = "LstResults" Me.LstResults.Size = New System.Drawing.Size(537, 212) Me.LstResults.TabIndex = 1 ' 'Label2 ' Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label2.Location = New System.Drawing.Point(16, 72) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(96, 16) Me.Label2.TabIndex = 2 Me.Label2.Text = "Cost of Ticket:" ' 'TxtTicket ' Me.TxtTicket.Location = New System.Drawing.Point(144, 72) Me.TxtTicket.Name = "TxtTicket" Me.TxtTicket.Size = New System.Drawing.Size(104, 20) Me.TxtTicket.TabIndex = 3 Me.TxtTicket.Text = "1.00" ' 'TxtGov ' Me.TxtGov.Location = New System.Drawing.Point(144, 104) Me.TxtGov.Name = "TxtGov" Me.TxtGov.Size = New System.Drawing.Size(104, 20) Me.TxtGov.TabIndex = 5 Me.TxtGov.Text = ".50" ' 'Label3 ' Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label3.Location = New System.Drawing.Point(16, 104) Me.Label3.Name = "Label3" Me.Label3.Size = New System.Drawing.Size(120, 16) Me.Label3.TabIndex = 4 Me.Label3.Text = "Governmet Share:" ' 'TxtTimes ' Me.TxtTimes.Location = New System.Drawing.Point(144, 136) Me.TxtTimes.Name = "TxtTimes" Me.TxtTimes.Size = New System.Drawing.Size(104, 20) Me.TxtTimes.TabIndex = 7 Me.TxtTimes.Text = "10" ' 'Label4 ' Me.Label4.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label4.Location = New System.Drawing.Point(16, 136) Me.Label4.Name = "Label4" Me.Label4.Size = New System.Drawing.Size(120, 16) Me.Label4.TabIndex = 6 Me.Label4.Text = "Time to Simulate:" ' 'BtnSim ' Me.BtnSim.Location = New System.Drawing.Point(8, 184) Me.BtnSim.Name = "BtnSim" Me.BtnSim.Size = New System.Drawing.Size(64, 24) Me.BtnSim.TabIndex = 8 Me.BtnSim.Text = "Simulate" ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(224, 184) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(64, 24) Me.Button1.TabIndex = 9 Me.Button1.Text = "Quit" ' 'Button2 ' Me.Button2.Location = New System.Drawing.Point(152, 184) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(64, 24) Me.Button2.TabIndex = 10 Me.Button2.Text = "Clear" ' 'TxtWinnings ' Me.TxtWinnings.Location = New System.Drawing.Point(136, 232) Me.TxtWinnings.Name = "TxtWinnings" Me.TxtWinnings.ReadOnly = True Me.TxtWinnings.Size = New System.Drawing.Size(96, 20) Me.TxtWinnings.TabIndex = 12 Me.TxtWinnings.Text = "" ' 'Label5 ' Me.Label5.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label5.Location = New System.Drawing.Point(16, 232) Me.Label5.Name = "Label5" Me.Label5.Size = New System.Drawing.Size(120, 16) Me.Label5.TabIndex = 11 Me.Label5.Text = "Total Winnings" ' 'TxtTake ' Me.TxtTake.Location = New System.Drawing.Point(136, 272) Me.TxtTake.Name = "TxtTake" Me.TxtTake.ReadOnly = True Me.TxtTake.Size = New System.Drawing.Size(96, 20) Me.TxtTake.TabIndex = 14 Me.TxtTake.Text = "" ' 'Label6 ' Me.Label6.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label6.Location = New System.Drawing.Point(16, 272) Me.Label6.Name = "Label6" Me.Label6.Size = New System.Drawing.Size(120, 16) Me.Label6.TabIndex = 13 Me.Label6.Text = "Total Government Take" ' 'RichTextBox1 ' Me.RichTextBox1.Location = New System.Drawing.Point(264, 288) Me.RichTextBox1.Name = "RichTextBox1" Me.RichTextBox1.Size = New System.Drawing.Size(520, 40) Me.RichTextBox1.TabIndex = 15 Me.RichTextBox1.Text = "RichTextBox1" Me.RichTextBox1.Visible = False ' 'BtnWrite ' Me.BtnWrite.Location = New System.Drawing.Point(80, 184) Me.BtnWrite.Name = "BtnWrite" Me.BtnWrite.Size = New System.Drawing.Size(64, 24) Me.BtnWrite.TabIndex = 16 Me.BtnWrite.Text = "Write" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(866, 332) Me.Controls.Add(Me.BtnWrite) Me.Controls.Add(Me.RichTextBox1) Me.Controls.Add(Me.TxtTake) Me.Controls.Add(Me.Label6) Me.Controls.Add(Me.TxtWinnings) Me.Controls.Add(Me.Label5) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.BtnSim) Me.Controls.Add(Me.TxtTimes) Me.Controls.Add(Me.Label4) Me.Controls.Add(Me.TxtGov) Me.Controls.Add(Me.Label3) Me.Controls.Add(Me.TxtTicket) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.LstResults) Me.Controls.Add(Me.Label1) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub #End Region Const normalNumCust As Double = 2000 Const startPot As Double = 1000 Const excitingPot As Double = 5000 Const extraCust As Double = 100 ' return whether there is a winner, given number of customers ' Done using random number generator Private Function isWinner(ByVal numC As Integer) As Boolean Const likelihoodOfWin As Double = 0.00001 Dim likelihoodOfAWinner As Double likelihoodOfAWinner = 1 - ((1 - likelihoodOfWin) ^ numC) Dim rand As Double = Rnd() If rand < likelihoodOfAWinner Then Return True Else Return False End If End Function Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.Close() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click TxtTicket.Text = "" TxtGov.Text = "" TxtTimes.Text = "" TxtWinnings.Text = "" TxtTake.Text = "" LstResults.Items.Clear() RichTextBox1.Text = "" End Sub Private Sub BtnSim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSim.Click Dim ticket As Double Dim govShare As Double Dim times As Integer Dim dataok As Boolean = True Dim numContestants As Integer Dim amountInPot As Double Dim prevPot As Double Dim amountToAdd As Double Dim amountNewRevenue As Double Dim totalWinnings As Double Dim totalProfits As Double = -1 * startPot ' take out the initial government contribution Dim currentWinnings As Double Dim currentProfits As Double RichTextBox1.Text = "" ' ensure lottery ticket price is ok If IsNumeric(TxtTicket.Text) Then ticket = Convert.ToDouble(TxtTicket.Text) If ticket < 0.05 Or ticket > 5.0 Then MsgBox("ticket price must be between .05 and 5.00") dataok = False End If Else MsgBox("ticket price must be numeric") dataok = False End If ' ensure government share is ok If IsNumeric(TxtGov.Text) Then govShare = Convert.ToDouble(TxtGov.Text) If govShare < 0.01 Or govShare > 0.99 Then MsgBox("government share must be between .01 and 0.99") dataok = False End If Else MsgBox("government share must be numeric") dataok = False End If ' ensure lottery drawings to simulate is ok If IsNumeric(TxtTimes.Text) Then times = Convert.ToInt32(TxtTimes.Text) If times < 1 Or times > 100 Then MsgBox("number of lottery drawings to simulate must be between 1 and 100") dataok = False End If Else MsgBox("number of lottery drawings to simulate must be numeric") dataok = False End If ' if got through all of that validation, then do the simulation amountInPot = startPot Dim cnt As Integer Dim presalePot As Double If dataok Then ' loop through drawings For cnt = 1 To times presalePot = amountInPot ' determine how many contestant will be in the next drawing If amountInPot > excitingPot Then numContestants = normalNumCust + Convert.ToInt32(((amountInPot - excitingPot) / 1000) * extraCust) Else numContestants = normalNumCust End If amountNewRevenue = numContestants * ticket currentProfits = amountNewRevenue * govShare amountToAdd = amountNewRevenue - currentProfits amountInPot = amountInPot + amountToAdd prevPot = amountInPot If isWinner(numContestants) Then ' we have a winner currentWinnings = amountInPot ' govt kicks in some money amountInPot = startPot totalProfits = totalProfits - startPot Else currentWinnings = 0 amountInPot = amountInPot ' + startPot End If totalWinnings = totalWinnings + currentWinnings totalProfits = totalProfits + currentProfits LstResults.Items.Add("Starting Pot: " & presalePot.ToString("c") _ & " Contestants: " & numContestants _ & " Gov: " & currentProfits.ToString("c") _ & " Pot: " & prevPot.ToString("c") _ & " Win: " & currentWinnings.ToString("C") _ & " Carryover: " & amountInPot.ToString("C")) RichTextBox1.Text = RichTextBox1.Text & Environment.NewLine & "Starting Pot: " & presalePot.ToString("c") _ & " Contestants: " & numContestants _ & " Gov: " & currentProfits.ToString("c") _ & " Pot: " & prevPot.ToString("C") _ & " Win: " & currentWinnings.ToString("C") _ & " Carryover: " & amountInPot.ToString("C") Next cnt TxtWinnings.Text = totalWinnings.ToString("c") TxtTake.Text = totalProfits.ToString("c") End If End Sub Private Sub BtnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnWrite.Click Dim writer As IO.StreamWriter writer = IO.File.CreateText("lotteryResultsCost" & TxtTicket.Text & "Share" & _ TxtGov.Text & "Time" & TxtTimes.Text & ".txt") Dim mon As Integer For mon = 0 To LstResults.Items.Count - 1 Dim curr As String curr = LstResults.Items.Item(mon) writer.WriteLine(curr) Next mon writer.Close() End Sub End Class